package com.sy.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用于操作封装Servlet对象到ThreadLocal中的工具类
 */
public class ServletHolderUtils {
    private static ThreadLocal<HttpServletRequest> curReq = new ThreadLocal<>();
    private static ThreadLocal<HttpServletResponse> curRes = new ThreadLocal<>();

    private ServletHolderUtils() {

    }

    /**
     * 获取当前线程的请求对象
     * @return 当前线程的请求对象
     */
    public static HttpServletRequest getCurRequest() {
        return curReq.get();
    }

    /**
     * 获取当前线程的响应对象
     * @return 当前线程的响应对象
     */
    public static HttpServletResponse getCurResponse() {
        return curRes.get();
    }

    /**
     * 用于保存当前的请求到ThreadLocal中
     * @param req
     */
    public static void setCurRequest(HttpServletRequest req) {
        curReq.set(req);
    }

    /**
     * 用于保存当前的响应到ThreadLocal中
     * @param res
     */
    public static void setCurResponse(HttpServletResponse res) {
        curRes.set(res);
    }

    /**
     * 移除绑定的请求对象
     */
    public static void removeCurReq() {
        curReq.remove();
    }

    /**
     * 移除绑定的响应对象
     */
    public static void removeCurRes() {
        curRes.remove();
    }
}
